<?php

    function warehouse_links($variables) {
        if (array_key_exists('id', $variables['attributes']) && $variables['attributes']['id'] == 'main-menu-links') {
            $pid = variable_get('menu_main_links_source', 'main-menu');
            $tree = menu_tree($pid);
            foreach ($tree as $key => $val) {
                if (is_numeric($key)) {
                    $tree[$key]["#attributes"]['id'] = 'menu_' . $key;
                    if ($key == 340) {
                        $tree[$key]["#attributes"]['class'][] = 'main-menu-dropdown';
                    }
                    $tree[$key]["#attributes"]['class'][] = 'li-main-menu';
                }
            }
            return drupal_render($tree);
        }
        return theme_links($variables);
    }

    function warehouse_menu_link(array $variables) {
        $element = $variables['element'];
        $sub_menu = '';
        $pre_title = '';
        $sub_title = '';
        $element['#localized_options']['html'] = TRUE;
        if ($element['#below']) {
            $sub_menu = drupal_render($element['#below']);
        }
        if ($element['#theme'] == 'menu_link__main_menu') {
            if ($element['#original_link']['mlid'] == 340) {
                if (function_exists('warehouse_profile_menu_html')) {
                    $sub_menu = warehouse_profile_menu_html();
                }
                $pre_title = '<i class="icon-chevron-right icon-white"></i>';
                $sub_title = '<b class="caret"></b>';
            }
            if ($element['#original_link']['mlid'] == 221) {
                $pre_title = '<i class="icon-star icon-white"></i>';
            }
        }
        $output = l($pre_title . $element['#title'] . $sub_title, $element['#href'], $element['#localized_options']);
        return '<li' . drupal_attributes($element['#attributes']) . ' >' . $output . $sub_menu . "</li>\n";
    }

    function warehouse_form_element_label($variables) {
        $element = $variables['element'];
        // This is also used in the installer, pre-database setup.
        $t = get_t();
        // If title and required marker are both empty, output no label.
        if ((!isset($element['#title']) || $element['#title'] === '') && empty($element['#required'])) {
            return '';
        }
        // If the element is required, a required marker is appended to the label.
        $required = !empty($element['#required']) ? theme('form_required_marker', array('element' => $element)) : '';
        $title = filter_xss_admin($element['#title']);
        $attributes = array();
        // Style the label as class option to display inline with the element.
        if ($element['#title_display'] == 'after') {
            $attributes['class'] = 'option';
        } // Show label only to screen readers to avoid disruption in visual flows.
        elseif ($element['#title_display'] == 'invisible') {
            $attributes['class'] = 'element-invisible';
        }
        if (!empty($element['#id'])) {
            $attributes['for'] = $element['#id'];
        }
        // The leading whitespace helps visually separate fields from inline labels.
        return ' <label' . drupal_attributes($attributes) . '>' . $t('!required !title', array('!required' => $required, '!title' => $title)) . "</label>\n";
    }

    /**
     * Implementaion of hook_js_alter()
     */
    function warehouse_js_alter(&$javascript) {
        $javascript['misc/jquery.js']['data'] = drupal_get_path('theme', 'warehouse') . '/js/jquery-1.7.1.min.js';
    }

    function warehouse_breadcrumb($vars) {
        $breadcrumb = $vars['breadcrumb'];
        // removes breadcrumb from frontpage and styles it
        if (!empty($breadcrumb) && !drupal_is_front_page()) {
            $breadcrumb[] = '<span>' . drupal_get_title() . '</span>';
            return '<div class="breadcrumb">' . implode(' » ', $breadcrumb) . "</div>";
        }
    }